
// note use if("r"==str[i+1])  not if('r'==str[i+1])


// transfer regular express new line identifer to real new line
// return the line number
macro TransRegExpMultLine(hbuf, str)
{
	len=strlen(str)
	len=len-1		// the regexp must be two string

	lnNumb=0
	iLstFind=0
	iLstR=0
	i=0;
	
	while(i<len)
	{
		if("\\"==str[i])
		{
			// find new line
			if( ("r"==str[i+1]) || ("n"==str[i+1]) || ("p"==str[i+1]) )
			{
				strLn=strmid(str, iLstFind, i)
				iLstFind=i
Msg(strLn)
				AppendBufLine (hbuf, strLn)
				lnNumb=lnNumb+1
				
				// \r
				if("r"==str[i+1])
				{
					iLstR=1
				}
				else
				{
					// if \r\n
					if( (1==iLstR) && ("n"==str[i+1]) )
					{
					iLstR=0
					}
				}
			}
			iLstFind=iLstFind+1
			i=i+1
		}
		else
		{
			iLstR=0
		}
		i=i+1
	}

	return lnNumb;	
}

macro MultiLineSearchInBuf(hbuf, strPat, fMatchCase, fWholeWordsOnly, fSel)
{
	hbufPat=NewBuf("MultiLineSearchPatten")
	iPatLnNumb=TransRegExpMultLine(hbufPat, strPat)

	iCurPat=1
	hbufRslt=NewBuf("MultiLineSearchResult")
	pat=GetBufLine(hbufPat, iCurPat)
	iCurPat++
	FirstRst=SearchInBufEx(hbuf, pat, 0, 0, fMatchCase, 1, fWholeWordsOnly, hbufRslt, 0)
	if(""==FirstRst)
	{
		return
	}
	lnLast=FirstRst.ln
	
	while(iCurPat<=iPatLnNumb)
	{
		pat=GetBufLine(hbufPat, iCurPat)
		iCurPat=iCurPat+1

		// new line means empty line
		if(""==pat)
		{
			// not empty line faild
			if(""!=GetBufLine(hbuf, lnLast+1)
			{
				break
			}
			lnLast=lnLast+1
		}
		else
		{
			LastRst=SearchInBufEx(hbuf, pat, lnLast+1, 0, fMatchCase, 1, fWholeWordsOnly, hbufRslt, 0)

			// can't find
			if(""==LastRst)
			{
				break
			}

			// find not just after the last find
			if( (LastRst.lnFirst!=lnLast+1) && (LastRst.ichFirst!=0) )
			{
				break
			}

			// find correct
			lnLast=lnLast+1
		}
	}

	// find ok
	if(iCurPat>iPatLnNumb)
	{
		FirstRst.lnLast=LastRst.lnLast
		FirstRst.ichLim=LastRst.ichLim
		
		if(fSel)
		{
			SelectRegion(hbuf, FirstRst.lnFirst, FirstRst.ichFirst, FirstRst.lnLast, FirstRst.ichLim)
		}
	}

	CloseBuf(hbufPat)
	CloseBuf(hbufRslt)
	
}

macro testML()
{
	hbuf=GetCurrentBuf()
	strPat=")\\r\\n{"
	MultiLineSearchInBuf(hbuf, strPat, 1, 1, 1)
}
